// SYBISCRIPT.cpp : Implementation of DLL Exports.


// Note: Proxy/Stub Information
//      To build a separate proxy/stub DLL, 
//      run nmake -f SYBISCRIPTps.mk in the project directory.

#include "stdafx.h"
#include "resource.h"
#include <initguid.h>
#include "SYBISCRIPT.h"
#include <Aclapi.h>

#include "SYBISCRIPT_i.c"
#include "ISybiScript.h"


CComModule _Module;



BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_SybiScript, CSybiScript)
END_OBJECT_MAP()

/////////////////////////////////////////////////////////////////////////////
// DLL Entry Point

extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
	/*
	FILE *file = fopen("c:\\temp\\d.txt", "at");
		fwrite( "contructor", 1, 1, file);
		fclose( file ); 
	*/

    if (dwReason == DLL_PROCESS_ATTACH)
    {
        _Module.Init(ObjectMap, hInstance, &LIBID_SYBISCRIPTLib);
        DisableThreadLibraryCalls(hInstance);
    }
    else if (dwReason == DLL_PROCESS_DETACH)
        _Module.Term();
    return TRUE;    // ok
}

/////////////////////////////////////////////////////////////////////////////
// Used to determine whether the DLL can be unloaded by OLE

STDAPI DllCanUnloadNow(void)
{
    return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// Returns a class factory to create an object of the requested type

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
{
    return _Module.GetClassObject(rclsid, riid, ppv);
}

/////////////////////////////////////////////////////////////////////////////
// DllRegisterServer - Adds entries to the system registry
ULONG CalculateACLSize(PACL pACLOld, PSID* ppSidArray, int nNumSids, LPVOID Unk, int nNumACEs)
{
	ULONG lACLSize = 0;

	try
	{
		if(ppSidArray != NULL)
		{
			while(nNumSids--)
			{
				if(!IsValidSid(ppSidArray[nNumSids])) {
					lACLSize = 0;
					goto leave;
				}
				lACLSize += GetLengthSid( ppSidArray[nNumSids] );
				lACLSize += sizeof(ACCESS_ALLOWED_ACE) - sizeof( ((ACCESS_ALLOWED_ACE*)0)->SidStart) ;
			}
		}
		lACLSize += sizeof(ACL);
leave:;
	}
	catch(...) {
		lACLSize = 0;
	}

	return lACLSize;
}

STDAPI DllRegisterServer(void)
{
    // registers object, typelib and all interfaces in typelib
    long result = _Module.RegisterServer(TRUE);

	CScriptEvaluation14DaysLimited eval;
	eval.setup();

	// setup access rights for registry key into which product key 
	// will be installed
	PSID psidEveryOne;

	SID_IDENTIFIER_AUTHORITY sidAuth = SECURITY_WORLD_SID_AUTHORITY;
	AllocateAndInitializeSid( &sidAuth, 1, SECURITY_WORLD_RID,
		0, 0, 0, 0, 0, 0, 0, &psidEveryOne );

	PSID psidArray[1] = {psidEveryOne};

	ULONG lACLSize = CalculateACLSize( NULL, psidArray, 1, NULL, 0 );

	PACL pDACL = (PACL) HeapAlloc(GetProcessHeap(), 0, lACLSize);
	InitializeAcl( pDACL, lACLSize, ACL_REVISION );

	AddAccessAllowedAce( pDACL, ACL_REVISION, STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL, psidArray[0] );

	HKEY hkey;
	BOOL bResult = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Classes\\CLSID\\{EB33C405-35B9-494C-9E2A-286051B11A24}\\InprocServer32", 0,
		KEY_ALL_ACCESS, &hkey);

	SetSecurityInfo(hkey, SE_REGISTRY_KEY, DACL_SECURITY_INFORMATION, NULL, NULL, pDACL, NULL);

	HeapFree(GetProcessHeap(), 0, pDACL );
	FreeSid(psidEveryOne);

	// set create time equal to current system time
	TCHAR szModuleName[_MAX_PATH] = {0};
	GetModuleFileName( _Module.GetModuleInstance(), szModuleName, sizeof(szModuleName) );

	FILETIME ftCreate;
	SYSTEMTIME stLocal;

	HANDLE hFile = CreateFile( szModuleName, FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE,
		NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hFile)
	{
		GetSystemTime(&stLocal);
		SystemTimeToFileTime(&stLocal, &ftCreate);

		SetFileTime(hFile, &ftCreate, NULL, NULL);
		CloseHandle(hFile);
	}

	return result;
}

/////////////////////////////////////////////////////////////////////////////
// DllUnregisterServer - Removes entries from the system registry

STDAPI DllUnregisterServer(void)
{
    return _Module.UnregisterServer(TRUE);
}


